#ifndef _ARRAYLIST_H_
#define _ARRAYLIST_H_

/**
 * Represents an array whose capacity grows as elements are added.
 */
typedef struct {
    unsigned int count;
    unsigned int capacity;
    void **data;
} arraylist_t;

/**
 * Creates a new arraylist with an initial capacity.
 */
arraylist_t *arraylist_create(unsigned int capacity);

/**
 * Destroys an arraylist and optionally its contents.
 */
void arraylist_destroy(arraylist_t *array, int freeContents);

/**
 * Adds data to the end of an arraylist.
 * Returns 0 on success.
 */
int arraylist_add(arraylist_t *array, void *data);

/**
 * Returns the element at index in the array.
 */
void *arraylist_get(arraylist_t *array, unsigned int index);

/**
 * Removes and returns the element at index from the array.
 */
void *arraylist_remove(arraylist_t *array, unsigned int index);

/**
 * Removes all elements from the arraylist and optionally frees them.
 */
void arraylist_clear(arraylist_t *array, int freeContents);

/**
 * Inserts an item at index.
 * Returns 0 on success.
 */
int arraylist_insert(arraylist_t *array, void *data, unsigned int index);

/**
 * Sets the data for the item at index and returns the old data.
 */
void *arraylist_set(arraylist_t *array, void *data, unsigned int index);

/**
 * Sets index to that of the first element in the list with the given value.
 * Returns 0 if the item was found, -1 if not (index is unchanged).
 */
int arraylist_find(arraylist_t *array, void *data, unsigned int *index);

/**
 * Sets index to that of the first element in the list that matches a given value.
 * A match is determined by the result of calling the comparer function for each item in the list.
 * The comparer should return 0 if the two items do not match and nonzero for a match.
 * Returns nonzero if the item was found, 0 if not (index is unchanged).
 */
int arraylist_search(arraylist_t *array, void *data, unsigned int *index, int (*comparer)(void *data1, void *data2));

#endif

